package linkwechat.secirityhandler;

import linkwechat.pojo.ReturnData;
import linkwechat.pojo.ReturnEnum;
import linkwechat.util.ResponseUtil;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// 认证失败的处理器
@Component
public class LoseHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        System.out.println("认证失败");
        ReturnData respResult = ReturnData.setResp(ReturnEnum.ERROR,null);
        if  (exception instanceof DisabledException) {
            respResult = ReturnData.setResp(ReturnEnum.AUTH_LOCKED,null);
        } else if (exception instanceof BadCredentialsException) {
            respResult = ReturnData.setResp(ReturnEnum.AUTH_ERROR,null);
        }
        ResponseUtil.ResWriter(request,response,respResult);

    }

}
